#include "euqt.h"
#include <QtDebug>
//~ #include <QScriptEngineDebugger>
//~ #include <QScriptSyntaxCheckResult>
#include <QtScript>
#include <QScriptValue>



/*alert
Copyright (c) 2008 Kent M. Hansen

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

static QScriptValue alert(QScriptContext *ctx, QScriptEngine *eng)
{
	QString msg = ctx->argument(0).toString();
	int icon = ctx->argument(1).toInt32();
	int type = ctx->argument(2).toInt32();
	QString title = ctx->argument(3).toString();
	QMessageBox::Icon qticon = QMessageBox::NoIcon;
	switch (icon)
	{
	case 0: qticon = QMessageBox::Critical; break;
	case 1: qticon = QMessageBox::Warning; break;
	case 2: qticon = QMessageBox::Question; break;
	case 3: qticon = QMessageBox::Information; break;
	}
	QMessageBox::StandardButtons buttons = QMessageBox::NoButton;
	switch (type)
	{
	case 0:
		buttons |= QMessageBox::Ok;
		break;
	case 1:
		buttons |= QMessageBox::Ok;
		buttons |= QMessageBox::Cancel;
		break;
	case 2:
		buttons |= QMessageBox::Yes;
		buttons |= QMessageBox::No;
		break;
	case 3:
		buttons |= QMessageBox::Yes;
		buttons |= QMessageBox::No;
		buttons |= QMessageBox::Cancel;
		break;
	}
	QMessageBox box(qticon, title, msg, buttons);
	box.exec();
	return eng->undefinedValue();
}
        //~ QScriptValue fun = eng.newFunction(alert);
        //~ QScriptValue proxy = eng.evaluate("argumentative")
                             //~ .call(QScriptValue(), QScriptValueList() << fun << descriptors);
        //~ eng.globalObject().setProperty("alert", proxy);
    //~ }
    //~ eng.evaluate("alert('Arguments passed list-style', 2, 1)");
    //~ eng.evaluate("alert( { cMsg: 'Arguments passed object literal-style', nIcon: 2, nType: 1 } )");


char * Seval_string = new char [4096];

extern "C"
QScriptEngine *qt_scriptengine_create()
{
	QScriptEngine *engine = new QScriptEngine();
        
	return engine;
}

extern "C"
int qt_scriptengine_eval_integer(QScriptEngine *engine, const char *s)
{
	//~ warning: converting to `int' from `qsreal'
	return (int) engine->evaluate(s).toNumber();
}


extern "C"
float qt_scriptengine_eval_float(QScriptEngine *engine, const char *s)
{
	//
	return (float) engine->evaluate(s).toNumber();
}

extern "C"
const char *qt_scriptengine_eval_string(QScriptEngine *engine, const char *s)
{
	QScriptValue result = engine->evaluate(s);
	if (engine->hasUncaughtException())
	{
		int line = engine->uncaughtExceptionLineNumber();
		qDebug() << "uncaught exception at line" << line << ":" << result.toString();
	}
	strlcpy(Seval_string, static_cast<const char*>(result.toString().toUtf8().data()), 2000);//shareddata
	return Seval_string;
}


extern "C"
const QScriptValue  *qt_scriptengine_eval_script(QScriptEngine *engine, const char *s)
{
	QScriptValue *result = new QScriptValue;
	*result = engine->evaluate(s);
	if (engine->hasUncaughtException())
	{
        QStringList bt = engine->uncaughtExceptionBacktrace();
        QString message = engine->uncaughtException().toString();
		int line = engine->uncaughtExceptionLineNumber();
		qDebug() << "uncaught exception at line" << line << ":\n";
        qWarning("%s\n%s", qPrintable(message), qPrintable(bt.join("\n")));
	    delete result;
	    return NULL;
	}
	return result;
}

//destroy

//~ You can check whether the evaluation caused an exception by calling hasUncaughtException(). In that case, you can call toString() on the error object to obtain an error message. The current uncaught exception is also available through uncaughtException().

    //~ QScriptValue globalObject = eng.globalObject();

